package com.zeldu.teacher.homework.util;

import com.alibaba.fastjson.JSON;
import com.zeldu.common.exception.CustomException;
import com.zeldu.common.model.response.CommonCode;


import com.zeldu.teacher.homework.entity.Student;
import com.zeldu.teacher.homework.entity.Teacher;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.OAuth2Authentication;

public  class JWTInfo {
    public static Student getStudentJwt(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if(!(authentication instanceof OAuth2Authentication)){
            return null;
        }
        OAuth2Authentication oAuth2Authentication = (OAuth2Authentication) authentication;
        Authentication userAuthentication = oAuth2Authentication.getUserAuthentication();
        //取出用户身份信息
        String principal = userAuthentication.getName();
        Student student=null;
        try {
            student = JSON.parseObject(principal, Student.class);
        }catch (Exception e){
            throw new CustomException(CommonCode.Wrong_User);
        }

        return student;
    }

    public static Teacher getTeacherJwt(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if(!(authentication instanceof OAuth2Authentication)){
            return null;
        }
        OAuth2Authentication oAuth2Authentication = (OAuth2Authentication) authentication;
        Authentication userAuthentication = oAuth2Authentication.getUserAuthentication();
        //取出用户身份信息
        String principal = userAuthentication.getName();
        Teacher teacher=null;
        try {
            teacher = JSON.parseObject(principal, Teacher.class);
        }catch (Exception e){
            throw new CustomException(CommonCode.Wrong_User);
        }

        return teacher;
    }




 }
